package com.three.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.three.core.entity.FrontUser;
import com.three.core.entity.ProductOrder;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.order.feign.AdminUserFeign;
import com.three.order.feign.UserFeign;
import com.three.order.mapper.ProductOrderMapper;
import com.three.order.service.OrderService;
import com.three.order.vo.SearchFormVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;


/**
 * @author ：
 * @date ：Created in 2025/3/11 15:34
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserFeign userFeign;

    @Resource
    private ProductOrderMapper productOrderMapper;

    @Autowired
    private AdminUserFeign adminUserFeign;

    @Autowired
    private  RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R<IPage<ProductOrder>> selectProductOrderByCondition(SearchFormVo searchFormVo) {
        //获取Token
        String token = request.getHeader("token");
        // 获取用户名
        Object user = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> byName = adminUserFeign.getByName((String) user);
        Integer userId = byName.getData().getUserId();
        Page<ProductOrder> page = new Page<>(searchFormVo.getCurrentPage(), searchFormVo.getPageSize());
        IPage<ProductOrder> p = productOrderMapper.selectProductOrderByCondition(page, searchFormVo.getStatusCode(), userId);
        return new R<>(200, "查询成功", p);
    }

    @Override
    public R<IPage<ProductOrder>> selectMobileProductOrderByCondition(SearchFormVo searchFormVo) {
        R<FrontUser> userInfo = userFeign.getUserInfo();
        FrontUser data = userInfo.getData();
        if (data != null) {
            Page<ProductOrder> page = new Page<>(searchFormVo.getPageNo(), searchFormVo.getPageSize());
            if (searchFormVo.getStatusCode() == 0) {
                searchFormVo.setStatusCode(null);
            }
            IPage<ProductOrder> p = productOrderMapper.selectMobileProductOrderByCondition(page, searchFormVo.getStatusCode(), data.getUserId());
            return new R<>(200, "查询成功", p);
        }
        return new R<>(400, "查询失败", null);
    }

    // 生成随机订单号
    private String generateRandomOrderNo() {
        Random random = new Random();
        long randomNumber = 1000000000000L + (long) (random.nextDouble() * 9000000000000L);
        return "product" + randomNumber;
    }

    @Override
    public R updateBuyStatus(Integer id) {
        ProductOrder productOrder = productOrderMapper.selectById(id);
        if (productOrder != null) {
            productOrder.setId(null);
            productOrder.setOrderNo(generateRandomOrderNo());
            productOrder.setCreateTime(LocalDateTime.now());
            productOrder.setUpdateTime(LocalDateTime.now());
            productOrder.setStatusCode(11);
            productOrderMapper.insert(productOrder);
            return new R(200, "成功", null);
        }
        return new R(400, "失败", null);
    }

    @Override
    public R<ProductOrder> selectOneOrder(Integer id) {
        ProductOrder productOrder = productOrderMapper.selectById(id);
        if (productOrder != null){
            return new R<>(200, "查询成功", productOrder);
        }
        return new R<>(400, "查询失败", null);
    }

    @Override
    public R deleteoOrderById(Integer id) {
        int i = productOrderMapper.deleteById(id);
        if (i > 0) {
            return new R(200, "删除成功", null);
        }
        return new R(500, "删除失败", null);
    }

    @Override
    public R updateReviewStatus(Integer id) {
        ProductOrder productOrder = productOrderMapper.selectById(id);
        if (productOrder != null) {
            int i = productOrderMapper.updateReviewStatus(id);
            if (i > 0) {
                return new R(200, "成功", null);
            }
        }
        return new R(400, "失败", null);
    }

    @Override
    public R updateStatus(Integer id) {
        ProductOrder productOrder = productOrderMapper.selectById(id);
        if (productOrder != null){
            int i = productOrderMapper.updateStatus(id);
            if (i > 0) {
                return new R(200, "收货成功", null);
            }
        }
            return new R(500, "收货失败", null);
    }

    @Override
    public R updateDeliveryStatus(Integer id) {
        ProductOrder productOrder = productOrderMapper.selectById(id);
        if (productOrder != null){
            int i = productOrderMapper.updateDeliveryStatus(id);
            if (i > 0) {
                return new R(200, "发货成功", null);
            }
        }
        return new R(500, "发货失败", null);
    }
}
